define([
  'jquery',
  'routers/Router',
  'views/users/UserProfile',
  'views/residences/ResidenceForm',
  'views/internals/UsersList',
  'views/internals/UserDetail',
  'views/internals/FamiliarDetail',
  'views/users/ShareView',
  'models/Residence',
  'collections/Internals',
  'models/Internal',
  'models/Familiar',
  'models/User'
], function($, Router, UserProfile, ResidenceForm, UsersList, UserDetail, FamiliarDetail, ShareView, Residence, Internals, Internal, Familiar, User){

  var AdminRouter = Router.extend({
    type: "admin",
    
    routes: {
      "":                                          "index",
      "!/profile":                                 "profile",
      "!/residence":                               "residence",
      "!/users":                                   "users",
      "!/user/:id":                                "user",
      "!/familiar/:id":                            "familiar",
      "!/upload":                                  "upload"
    },

    familiar: function(id) {
      var familiar = new Familiar({id: id});
      var familiarDetail = new FamiliarDetail({model: familiar});;      
      $("#app").html(familiarDetail.render().el);      

      familiar.fetch();
    },
    
    index: function() {
      this.users();  
    },

    profile: function() {
      this.Logger.debug("Familiar router, profile route!");
      var user = new User();
      var profile = new UserProfile({model: user});
      $("#app").html(profile.render().el);

      user.fetch();
    },
    
    residence: function() {
      console.log("Residence route!");
      var residence = new Residence();
      var residenceForm = new ResidenceForm({model: residence});
      $("#app").html(residenceForm.render().el);

      residence.fetch();
    },

    users: function(tab) {
      console.log("Users!");
      var users = new Internals();
      users.pagination.setLimit(5);
      var usersList = new UsersList({messages: this.messages, collection: users});
      $("#app").html(usersList.render().el);
      
      users.fetch();
      //var usersTab = new UsersTab({collection: users});
      
      //var uploadView = new UploadView({messages: this.messages});
    },

    user: function(id) {
      console.log("Detail of users with id: " + id);
      var user = new Internal({id: id});
      var view = new UserDetail({messages: this.messages, model: user});
      $("#app").html(view.render().el);

      user.fetch();
    },

    upload: function() {
      this.Logger.debug("User router, upload route!");
      var user = new User();
      var shareView = new ShareView({el: "#app", model: user, type: this.type, messages: this.messages});
      shareView.render();

      //user.internals.fetch();
    }

  });

  return AdminRouter;
});
